package com.jxit0573.android.lib.util;

import android.content.Context;
import android.content.SharedPreferences;

import androidx.annotation.Nullable;

/**
 * @author jia
 */
public class SharedPreferencesUtils {

    private static volatile SharedPreferencesUtils instance;
    private static SharedPreferences sharedPreferences;
    private static SharedPreferences.Editor editor;
    private static String spName;

    private SharedPreferencesUtils() {
    }

    private SharedPreferencesUtils(Context context) {
        this(context, context.getPackageName());
        spName = context.getPackageName();
    }

    private SharedPreferencesUtils(Context context, String name) {
        spName = name;
        sharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
    }

    public static SharedPreferencesUtils getInstance(Context context) {
        if (instance == null || !context.getPackageName().equals(spName)) {
            instance = new SharedPreferencesUtils(context);
        }
        return instance;
    }

    public static SharedPreferencesUtils getInstance(Context context, String name) {
        if (instance == null || !spName.equals(name)) {
            instance = new SharedPreferencesUtils(context, name);
        }
        return instance;
    }

    public SharedPreferencesUtils putString(String key, @Nullable String value) {
        editor.putString(key, value);
        editor.apply();
        return instance;
    }

    public SharedPreferencesUtils putInt(String key, int value) {
        editor.putInt(key, value);
        editor.apply();
        return instance;
    }

    public SharedPreferencesUtils putBoolean(String key, boolean value) {
        editor.putBoolean(key, value);
        editor.apply();
        return instance;
    }

    public SharedPreferencesUtils putFloat(String key, float value) {
        editor.putFloat(key, value);
        editor.apply();
        return instance;
    }

    public SharedPreferencesUtils putLong(String key, long value) {
        editor.putLong(key, value);
        editor.apply();
        return instance;
    }

    public SharedPreferencesUtils remove(String key) {
        editor.remove(key);
        editor.apply();
        return instance;
    }

    public SharedPreferencesUtils clear() {
        editor.clear();
        editor.apply();
        return instance;
    }

    public String getString(String key) {
        return sharedPreferences.getString(key, null);
    }

    public String getString(String key, String defValue) {
        return sharedPreferences.getString(key, defValue);
    }

    public int getInt(String key) {
        return sharedPreferences.getInt(key, 0);
    }

    public int getInt(String key, int defValue) {
        return sharedPreferences.getInt(key, defValue);
    }

    public boolean getBooleanTrue(String key) {
        return sharedPreferences.getBoolean(key, true);
    }

    public boolean getBooleanFalse(String key) {
        return sharedPreferences.getBoolean(key, false);
    }

    public float getFloat(String key) {
        return sharedPreferences.getFloat(key, 0.0f);
    }

    public float getFloat(String key, float defValue) {
        return sharedPreferences.getFloat(key, defValue);
    }

    public long getLong(String key) {
        return sharedPreferences.getLong(key, 0L);
    }

    public long getLong(String key, long defValue) {
        return sharedPreferences.getLong(key, defValue);
    }
}
